'use strict';
angular.module('CcxiApp', []);
app.filter('textLengthSet', function() {
    return function(value, wordwise, max, tail) {
        if (!value) return '';

        max = parseInt(max, 10);
        if (!max) return value;
        if (value.length <= max) return value;

        value = value.substr(0, max);
        if (wordwise) {
            var lastspace = value.lastIndexOf(' ');
            if (lastspace != -1) {
                value = value.substr(0, lastspace);
            }
        }

        return value + (tail || ' …');//'...'可以换成其它文字
    };
});

//营业额成交量图表展示
app.controller('statisticsCtrl',['$scope','homeService','toaster', function($scope,homeService,toaster) {

    $scope.legend = ["营业额(元)", "下单数(笔)"];
    $scope.item = [ '--','--','--','--','--','--','--' ];
    $scope.data = [
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    ];

    $scope.ordersMonth=new Date().getMonth()+1;
    $scope.ordersYears=new Date().getFullYear();
    $scope.months=[];
    $scope.years=[];

    var getYear=function(){
        for(var i=2015;i<$scope.ordersYears;i++){
            $scope.years.push(i+1);
        }
    }

    var getMonth=function(){
        $scope.months=[];
        var nowYear=new Date().getFullYear();
        $scope.ordersMonth=($scope.ordersYears==nowYear)?(new Date().getMonth()+1):12;
        for(var i=0;i<$scope.ordersMonth;i++){
            $scope.months.push(i+1);
        }
    }

    getYear();
    getMonth();

    //查询统计信息
    $scope.queryOrdersInfo=function(){
        homeService.queryOrdersInfo($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.item=result.data.item;
                    $scope.data=result.data.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryOrdersInfo();

    $scope.changeMonth=function(){
        $scope.queryOrdersInfo();
    }

    $scope.changeYear=function(){
        getMonth();
        $scope.queryOrdersInfo();
    }



    $scope.statistics={
        totalUser:0,
        totalShop:0,
        todayShopApproval:0,
        todayGoodApproval:0
    };
    $scope.query= function(){
        homeService.find($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.statistics=result.data.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.query();

}]);



app.controller('miniAppsCtrl',['$scope','homeService','$rootScope', function($scope,homeService,$rootScope) {
    $scope.legend = ["打开次数", "访问次数","访问人数","新用户量","人均停留时长(秒)","次均停留时长(秒)"];
    $scope.item = [ '--','--','--','--','--','--','--','--','--','--','--','--' ];
    $scope.data = [
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    ];
    $scope.date = [
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    ];

    $scope.flowMonth=new Date().getMonth()+1;
    $scope.flowYears=new Date().getFullYear();
    $scope.months=[];
    $scope.years=[];

    var getYear=function(){
        for(var i=2015;i<$scope.flowYears;i++){
            $scope.years.push(i+1);
        }
    }

    var getMonth=function(){
        $scope.months=[];
        var nowYear=new Date().getFullYear();
        $scope.flowMonth=($scope.flowYears==nowYear)?(new Date().getMonth()+1):12;
        for(var i=0;i<$scope.flowMonth;i++){
            $scope.months.push(i+1);
        }
    };

    getYear();
    getMonth();

    //查询小程序流量统计信息
    $scope.queryFlowHistory= function(){
        homeService.queryFlowHistory($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.item=result.data.item;
                    $scope.data=result.data.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.queryFlowHistory();

    $scope.alterMonth=function(){
        $scope.queryFlowHistory();
    }

    $scope.alterYear=function(){
        getMonth();
        $scope.queryFlowHistory();
    }

}]);



app.controller('noticesInfo',['$scope','homeService','toaster', function($scope,homeService,toaster) {
    //显示系统公告
    $scope.queryInternalNotice=function(){
        homeService.queryInternalNotice($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.notices=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.queryInternalNotice();
}]);

app.directive('line', function() {
    return {
        scope: {
            id: "@",
            legend: "=",
            item: "=",
            data: "="
        },
        restrict: 'E',
        template: '<div style="height:400px;width: 100%;"></div>',
        replace: true,
        link: function($scope, element, attrs, controller) {
            var option = {
                // 提示框，鼠标悬浮交互时的信息提示
                tooltip: {
                    trigger: 'axis',
                    textStyle:{
                        color:'#fff'
                    },
                    fontSize:12,
                    padding:15,
                    formatter:function(params){
                        var res='';
                        var date='';
                        var one='';
                        var two='';
                        var three='';
                        var four='';
                        var five='';
                        var six='';
                        for(var i=0;i<params.length;i++){
                            date=params[0].name+'</br>';
                            if(params[i].seriesIndex==0){
                                one=params[i].seriesName+" : "+params[i].data+'</br>';
                            }else if(params[i].seriesIndex==1){
                                two=params[i].seriesName+" : "+params[i].data+'</br>';
                            }else if(params[i].seriesIndex==2){
                                three=params[i].seriesName+" : "+params[i].data+'</br>';
                            }else if(params[i].seriesIndex==3){
                                four=params[i].seriesName+" : "+params[i].data+'</br>';
                            }else if(params[i].seriesIndex==4){
                                five=params[i].seriesName+" : "+params[i].data+'</br>';
                            }else if(params[i].seriesIndex==5){
                                six=params[i].seriesName+" : "+params[i].data;
                            }

                        }
                        res=date+one+two+three+four+five+six;
                        return res;
                    }
                },
                // 图例
                legend: {
                    data: $scope.legend
                },
                // 横轴坐标轴
                xAxis: [{
                    type: 'category',
                    silent:true,
                    axisTick: {
                        alignWithLabel: true
                    },
                    data: $scope.item
                }],
                // 纵轴坐标轴
                yAxis: [{
                    type: 'value'
                }],
                // 数据内容数组
                series: function(){
                    var serie=[];
                    for(var i=0;i<$scope.legend.length;i++){
                        var item = {
                            name : $scope.legend[i],
                            type: 'line',
                            smooth: true,
                            data: $scope.data[i]
                        };
                        serie.push(item);
                    }
                    return serie;
                }
            };
            var myChart = echarts.init(document.getElementById($scope.id),'macarons');
            myChart.setOption(option);

            $scope.$watch('item',function(){
                option.xAxis=[{type: 'category', data: $scope.item}];
                var serie=[];
                for(var i=0;i<$scope.legend.length;i++){
                    var item = {
                        name : $scope.legend[i],
                        type: 'line',
                        data: $scope.data[i]
                    };
                    serie.push(item);
                }
                option.series=serie;
                myChart.clear();
                myChart.setOption(option,true);
            })
        }
    };
});